﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using KegCommunication;
using KegData;

namespace KegManager
{
    /// <summary>
    /// Interaction logic for PourWindow.xaml
    /// </summary>
    public partial class PourWindow : Window
    {
        public int CloseDelay = 3000;

        public static readonly DependencyProperty VolumeProperty = DependencyProperty.Register("Volume", typeof(double), typeof(PourWindow));
        public static readonly DependencyProperty UserProperty = DependencyProperty.Register("User", typeof(User), typeof(PourWindow));
        public static readonly DependencyProperty TapProperty = DependencyProperty.Register("Tap", typeof(Tap), typeof(PourWindow));

        public double Volume
        {
            get { return (double)GetValue(VolumeProperty); }
            set { SetValue(VolumeProperty, value); }
        }

        public User User
        {
            get { return (User)GetValue(UserProperty); }
            set { SetValue(UserProperty, value); }
        }

        public Tap Tap
        {
            get { return (Tap)GetValue(TapProperty); }
            set { SetValue(TapProperty, value); }
        }

        public PourWindow()
        {
            InitializeComponent();
        }
        
        public void SetTap(Tap t)
        {
            Volume = 0;
            Tap = t;
        }

        public void OnUserAuthenticated(User user)
        {
            User = user;
            // Store the user 
        }

        public void OnStartPour(PourEventArgs pe)
        {
            // TODO: What do I do about pour noise?

            Volume = pe.Volume;
        }

        public void OnContinuePour(PourEventArgs pe)
        {
            Volume = pe.Volume;

            // Only show this window after at least an ounce has been poured
            if (Volume > 0.03)
            {
                Dispatcher.Invoke(
                DispatcherPriority.Normal,
                (Action)delegate()
                {
                    this.Show();
                }
             ); 
            }
        }

        public void OnEndPour(PourEventArgs pe)
        {
            Volume = pe.Volume;       

            // Spawn a timer that will close the window after a certain time has passed
            (new Thread(new ThreadStart(CloseWindow))).Start();
        }

        private void CloseWindow()
        {
            Thread.Sleep(CloseDelay);
            Dispatcher.Invoke(
                DispatcherPriority.Normal,
                (Action)delegate()
                {
                    this.Hide();
                    Volume = 0;
                }
             );
        }

        //private void FormFadeOut_Completed(object sender, EventArgs e)
        //{
        //    this.Hide();
        //    this.Opacity = 1.0f;
        //}
    }
}
